package com.zy.utils

import android.animation.Animator
import com.zy.utils.AnimatorUtils.getMove
import com.zy.utils.AnimatorUtils.getReset
import com.zy.utils.AnimatorUtils.getTogetherStart
import com.zy.utils.ScreenChangeUtils.PointNode
import com.zy.utils.ScreenChangeUtils.ScreenChangeListener
import android.animation.AnimatorListenerAdapter
import android.view.View
import java.util.ArrayList

object ScreenChangeUtils {
    fun screenOutOrInAnimator(
        childContainer: View,
        pointNode: List<PointNode>,
        out: Boolean,
        listener: ScreenChangeListener
    ) {
        val animatorList: MutableList<Animator> = ArrayList()
        for (node in pointNode) {
            val pointAnimator: Animator = if (out) {
                getMove(node.origin, node.target)
            } else {
                getReset(node.origin)
            }
            animatorList.add(pointAnimator)
        }
        val animator = getTogetherStart(animatorList)
        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationStart(animation: Animator, isReverse: Boolean) {
                super.onAnimationStart(animation, isReverse)
                if (!out) {
                    childContainer.visibility = View.INVISIBLE
                }
            }

            override fun onAnimationEnd(animation: Animator, isReverse: Boolean) {
                super.onAnimationEnd(animation, isReverse)
                if (out) {
                    childContainer.visibility = View.VISIBLE
                }
                listener.screenEnd()
            }
        })
        animator.start()
    }

    class PointNode(var origin: View, var target: View)
    interface ScreenChangeListener {
        fun screenEnd()
    }
}